package com.first.config.redis.spring;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.first.config.redis.JredisSession;

@Configuration
@ConditionalOnClass(JredisSession.class)
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class SessionConfig implements ConfigureRedisAction{
// 不知道ConfigureRedisAction 有什么用 也可以不继承
	@Bean
	public  ConfigureRedisAction configureRedisAction() {
//	解决BUG：	Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' 
	    return org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP;
	}
	
	@Override
	public void configure(RedisConnection connection) {
		System.out.println("w caojhdis ado as");
	}

	
//	@Bean("sessionRedisTemplate")
//	@Primary
//	public RedisTemplate<Object, Object> sessionRedisTemplate(
//			@Qualifier("JedisConnectionFactory")JedisConnectionFactory connectionFactory) {
//		
//		RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
//		template.setConnectionFactory(connectionFactory);
//		
//		template.setKeySerializer(new StringRedisSerializer());
//		template.setHashKeySerializer(new StringRedisSerializer());
//		
//		
//        //定义value的序列化方式
//        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//        
//        template.setValueSerializer(jackson2JsonRedisSerializer);
//        template.setHashValueSerializer(jackson2JsonRedisSerializer);
//        template.afterPropertiesSet();
//		
//		return template;
//	}
}
